package indi.zhifa.engine.test2.busylogic.crudtest.service.impl;

import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import indi.zhifa.core.mpenhance.utils.DbDtoEntityUtil;

import indi.zhifa.engine.test2.busylogic.crudtest.entity.TestEntityDto;
import indi.zhifa.engine.test2.busylogic.crudtest.service.ITestCurdService;
import indi.zhifa.engine.test2.config.DataSourceConfig;
import indi.zhifa.engine.test2.gen.busy.test.dbservice.IGenBusyTestDbService;
import indi.zhifa.engine.test2.gen.busy.test.entity.GenBusyTestEntity;
import indi.zhifa.engine.test2.gen.monitor.test.dbservice.IGenMonitorTestDbService;
import indi.zhifa.engine.test2.gen.monitor.test.entity.GenMonitorTestEntity;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@RequiredArgsConstructor
@Service
public class TestCurdServiceImpl implements ITestCurdService {

    private final IGenMonitorTestDbService mGenMonitorTestDbService;
    private final IGenBusyTestDbService mGenBusyTestDbService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void mpSave(TestEntityDto pTestDto) {
        GenMonitorTestEntity genMonitorTestEntity = DbDtoEntityUtil.createFromDto(pTestDto,GenMonitorTestEntity.class);
        GenBusyTestEntity genBusyTestEntity = DbDtoEntityUtil.createFromDto(pTestDto,GenBusyTestEntity.class);
        genBusyTestEntity.setEnpId(0L);
        genBusyTestEntity.setEnpCode("sys");
        genMonitorTestEntity.setEnpId(0L);
        genMonitorTestEntity.setEnpCode("sys");

        DataSourceConfig.MultiDataSource.changeDb("monitor");
        mGenMonitorTestDbService.save(genMonitorTestEntity);
        DataSourceConfig.MultiDataSource.changeDb("busy001");
        mGenBusyTestDbService.save(genBusyTestEntity);
        DataSourceConfig.MultiDataSource.changeDb("busy002");
        mGenBusyTestDbService.save(genBusyTestEntity);
    }

}
